﻿using Cinch;
using FR.SAFTAnalyser.Model;
using MEFedMVVM.Common;
using System.Linq;

namespace FR.SAFTAnalyser.ViewModels
{
    public class ProdutsViewModel : WorkspaceViewModel
    {
        public ProdutsViewModel()
        {
            if (!Designer.IsInDesignMode)
            {
                Mediator.Instance.Register(this);
                this.DisplayName = "Produtos";
            }
        }

        #region Mediator Messages
        [MediatorMessageSink(MessageType.SAFT_FILE_OPENED)]
        private void LoadProducts(AuditFile saft)
        {
            if (saft != null && saft.MasterFiles != null)
                this.Products = saft.MasterFiles.Product;
        }

        [MediatorMessageSink(MessageType.FOCUS_PRODUCT)]
        private void LoadProducts(string pk)
        {
            if (!string.IsNullOrEmpty(pk) && Products != null)
            {
                var productWithError = from p in Products
                                       where p.Pk.AreEqualIgnoreCase(pk)
                                       select p;

                if (productWithError != null)
                    Product = productWithError.FirstOrDefault();
            }
        }
        #endregion Mediator Messages

        #region BindingProperties

        Product[] products;
        public Product[] Products
        {
            get { return products; }
            set
            {
                products = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<ProdutsViewModel>(x => x.Products));
            }
        }

        Product product;
        public Product Product
        {
            get { return product; }
            set
            {
                product = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<ProdutsViewModel>(x => x.Product));
            }
        } 

        string filter;
        public string Filter
        {
            get { return filter; }
            set
            {
                filter = value;

                if (string.IsNullOrEmpty(filter))
                    this.Products = SAFT.SaftAnalyser.Instance.SaftFile.MasterFiles.Product;
                else
                {
                    this.Products = (from c in SAFT.SaftAnalyser.Instance.SaftFile.MasterFiles.Product
                                     where !string.IsNullOrEmpty(c.ProductCode) && c.ProductCode.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                         !string.IsNullOrEmpty(c.ProductDescription) && c.ProductDescription.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                         !string.IsNullOrEmpty(c.ProductGroup) && c.ProductGroup.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                         !string.IsNullOrEmpty(c.ProductNumberCode) && c.ProductNumberCode.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                         c.ProductType.ToString().IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0
                                      select c).ToArray();
                }
            }
        }

        #endregion BindingProperties
    }
}
